package com.shixianjun.leetcode;

import java.util.HashSet;
import java.util.Set;

public class _3_无重复字符的最长子串 {

    static public int lengthOfLongestSubstring(String s) {
        /**
         * 第一想法 暴力美学 每个字符为开头 向后遍历  记录最长
         * 123341
         * */

        if (s.length() <= 1) {
            return s.length();
        }

        int maxLength = 0;
        Set<Character> pool = new HashSet<Character>();
        for (int i = 0; i < s.length() - 1; i++) {
            pool.clear();
            Character sub = s.charAt(i);
            pool.add(sub);
            for (int j = i + 1; j < s.length(); j++) {
                Character subs = s.charAt(j);
                if (pool.contains(subs)) {
                    break;
                } else {
                    pool.add(subs);
                }
            }
            maxLength = Math.max(maxLength, pool.size());
        }

        return maxLength;
    }

    static public int lengthOfLongestSubstring2(String s) {
        /**
         * 第一想法 暴力美学 每个字符为开头 向后遍历  记录最长
         * 123341
         * */

        if (s.length() <= 1) {
            return s.length();
        }

        int maxLength = 0;
        Set<Character> pool = new HashSet<Character>();
        for (int i = 0; i < s.length() - 1; i++) {
            pool.clear();
            Character sub = s.charAt(i);
            pool.add(sub);
            for (int j = i + 1; j < s.length(); j++) {
                Character subs = s.charAt(j);
                if (pool.contains(subs)) {
                    break;
                } else {
                    pool.add(subs);
                }
            }
            maxLength = Math.max(maxLength, pool.size());
        }

        return maxLength;
    }

    public static void main(String[] args) {
        int length = lengthOfLongestSubstring2(" ");
        System.out.print(length);
    }
}
